(function($) {
	var groupCode = '',groupDesc = '',locId = '',locDesc = '';
	var absentCodeArr = [];
	var userpicker
	$.init({
		swipeBack: true //启用右滑关闭功能
	});
	$.plusReady(function() {
		var liStDate = document.getElementById('li-stDate');
		var liEndDate = document.getElementById('li-endDate');
		if (plus.os.name == 'iOS') {
			userpicker = new mui.xhPicker(1,'请选择','pickerId');
			liStDate.addEventListener('tap', function() {
				iosDateShow('li-stDate', 'stDate');
			}, false);
			liEndDate.addEventListener('tap', function() {
				iosDateShow('li-endDate', 'endDate');
			}, false);
		}else{
			userpicker = new mui.PopPicker();
			liStDate.addEventListener('tap', function() {
				liStEndDateClicked('li-stDate', 'stDate');
			}, false);
			liEndDate.addEventListener('tap', function() {
				liStEndDateClicked('li-endDate', 'endDate');
			}, false);
		}
		var currentLoc = getLocalData('currentLoc');
		if(currentLoc) {
			groupCode = currentLoc['GroupCode'];
			groupDesc = currentLoc['GroupDesc'];
			locId = currentLoc['LocId'];
			locDesc = currentLoc['LocDesc'];
		} else {
			mui.toast('用户未登录');
			mui.openWindow('logon-shell.html', 'logon-shell');
			return;
		}
		var liAbsentCode = document.getElementById('li-absentCode');
		liAbsentCode.addEventListener('tap', liAbsentCodeClicked, false);

		var liDateLen = document.getElementById('li-dateLen');
		liDateLen.addEventListener('tap', liDateLenClicked, false);

		var submitBtn = document.getElementById('submitBtn');
		submitBtn.addEventListener('tap', submitBtnClicked, false);
	});
	jQuery("#textarea").on("input propertychange", function() {
		var $this = jQuery(this),
			_val = $this.val(),
			count = "";
		if(_val.length > 100) {
			$this.val(_val.substring(0, 100));
			mui.toast('最多输入100个字符')
		}
		count = $this.val().length;
		jQuery("#text-count").text(count);
	});

	function liStEndDateClicked(liId, spanId) {
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		var li = document.getElementById(liId);
		var _self = li;
		if(_self.picker) {
			_self.picker.show(function(rs) {
				var dateStr = rs.text;
				var stDate = document.getElementById('stDate');
				var stDateStr = stDate.innerText;
				var endDate = document.getElementById('endDate');
				var endDateStr = endDate.innerText;
				if(spanId == 'endDate') {
					if(stDateStr != '请选择' && dateStr < stDateStr) {
						mui.toast('结束日期不能小于开始日期');
						_self.picker.dispose();
						_self.picker = null;
						liStEndDateClicked(liId, spanId)
						return;
					}
				} else {
					if(endDateStr != '请选择' && dateStr > endDateStr) {
						mui.toast('请选择结束日期');
						stDate.innerText = dateStr;
						computeAbsentLength();
						_self.picker.dispose();
						_self.picker = null;
						liStEndDateClicked('li-endDate', 'endDate')
						return;
					}
				}
				var spanele = document.getElementById(spanId);
				spanele.innerText = dateStr;
				_self.picker.dispose();
				_self.picker = null;
				computeAbsentLength();
			});
		} else {
			var optionsJson = '{"type":"date"}';
			var options = JSON.parse(optionsJson);
			_self.picker = new mui.DtPicker(options);
			_self.picker.show(function(rs) {
				var dateStr = rs.text;
				var stDate = document.getElementById('stDate');
				var stDateStr = stDate.innerText;
				var endDate = document.getElementById('endDate');
				var endDateStr = endDate.innerText;
				if(spanId == 'endDate') {
					if(stDateStr != '请选择' && dateStr < stDateStr) {
						mui.toast('结束日期不能小于开始日期');
						_self.picker.dispose();
						_self.picker = null;
						liStEndDateClicked(liId, spanId)
						return;
					}
				} else {
					if(endDateStr != '请选择' && dateStr > endDateStr) {
						mui.toast('请选择结束日期');
						stDate.innerText = dateStr;
						_self.picker.dispose();
						_self.picker = null;
						computeAbsentLength();
						liStEndDateClicked('li-endDate', 'endDate')
						return;
					}
				}
				var spanele = document.getElementById(spanId);
				spanele.innerText = dateStr;
				_self.picker.dispose();
				_self.picker = null;
				computeAbsentLength();
			});
		}
	}
	function iosDateShow(liId,spanId){
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		var li = document.getElementById(liId);
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			var stDate = document.getElementById('stDate');
			var stDateStr = stDate.innerText;
			var endDate = document.getElementById('endDate');
			var endDateStr = endDate.innerText;
			if(spanId == 'endDate') {
				if(stDateStr != '请选择' && dateStr < stDateStr) {
					mui.toast('结束日期不能小于开始日期');
					iosDateShow(liId, spanId)
					return;
				}
			} else {
				if(endDateStr != '请选择' && dateStr > endDateStr) {
					mui.toast('请选择结束日期');
					stDate.innerText = dateStr;
					computeAbsentLength();
					iosDateShow('li-endDate', 'endDate')
					return;
				}
			}
			var spanele = document.getElementById(spanId);
			spanele.innerText = dateStr;
			computeAbsentLength();
		}, function(e) {}, {});
	}
	/*
	 * 计算请假时长
	 */
	function computeAbsentLength() {
		var stDate = document.getElementById('stDate');
		var stDateStr = stDate.innerText;
		var endDate = document.getElementById('endDate');
		var endDateStr = endDate.innerText;
		if(stDateStr == '请选择' || endDateStr == '请选择') {
			return;
		}
		var dayCount = computeDatesInterval(stDateStr, endDateStr);
		var dateLen = document.getElementById('dateLen');
		dateLen.innerHTML = dayCount + '天';
	}

	function liAbsentCodeClicked() {
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		if(absentCodeArr.length > 0) {
			if (plus.os.name == 'iOS') {
				userpicker.setData(absentCodeArr,"请选择请假类型");
				userpicker.show(function(items,index) {
					var absentCodeE = document.getElementById('absentCode');
					var absentCodeStr = items[0]['HolidayDesc'];
					while(absentCodeStr.indexOf('+') >= 0) {
						absentCodeStr = absentCodeStr.replace('++', ' ');
						absentCodeStr = absentCodeStr.replace('+', ' ');
					}
					absentCodeE.innerText = absentCodeStr;
					//var index = this.pickers[0].getSelectedIndex();
					absentCodeE.setAttribute('data-index', index);
				});
			}else{
				userpicker.setData(absentCodeArr);
				userpicker.show(function(items) {
					var absentCodeE = document.getElementById('absentCode');
					var absentCodeStr = items[0]['HolidayDesc'];
					while(absentCodeStr.indexOf('+') >= 0) {
						absentCodeStr = absentCodeStr.replace('++', ' ');
						absentCodeStr = absentCodeStr.replace('+', ' ');
					}
					absentCodeE.innerText = absentCodeStr;
					var index = this.pickers[0].getSelectedIndex();
					absentCodeE.setAttribute('data-index', index);
				});
			}
			return;
		}
		plus.nativeUI.showWaiting("获取排班代码",{modal:false});
		var xmlStr = '<Request>' + locId + '</Request>';
		postHttpRequest(xmlStr, 'nurse.personal.holiday', function(result) {
			plus.nativeUI.closeWaiting();
			if(result) {
				absentCodeArr = eval('(' + result + ')');
				for(var i = 0; i < absentCodeArr.length; i++) {
					var oneDict = absentCodeArr[i];
					var absentCodeStr = oneDict['HolidayDesc'];
					while(absentCodeStr.indexOf('+') >= 0) {
						absentCodeStr = absentCodeStr.replace('++', ' ');
						absentCodeStr = absentCodeStr.replace('+', ' ');
					}
					absentCodeArr[i]['text'] = absentCodeStr;
				}
				if (plus.os.name == 'iOS') {
					userpicker.setData(absentCodeArr,"请选择请假类型");
					userpicker.show(function(items,index) {
						var absentCodeE = document.getElementById('absentCode');
						var absentCodeStr = items[0]['HolidayDesc'];
						while(absentCodeStr.indexOf('+') >= 0) {
							absentCodeStr = absentCodeStr.replace('++', ' ');
							absentCodeStr = absentCodeStr.replace('+', ' ');
						}
						absentCodeE.innerText = absentCodeStr;
						//var index = this.pickers[0].getSelectedIndex();
						absentCodeE.setAttribute('data-index', index);
					});
				}else{
					userpicker.setData(absentCodeArr);
					userpicker.show(function(items) {
						var absentCodeE = document.getElementById('absentCode');
						var absentCodeStr = items[0]['HolidayDesc'];
						while(absentCodeStr.indexOf('+') >= 0) {
							absentCodeStr = absentCodeStr.replace('++', ' ');
							absentCodeStr = absentCodeStr.replace('+', ' ');
						}
						absentCodeE.innerText = absentCodeStr;
						var index = this.pickers[0].getSelectedIndex();
						absentCodeE.setAttribute('data-index', index);
					});
				}
			} else {
				mui.toast("未能获取到排班岗位数据（result为空）");
			}
		}, function(errorStr) {
			mui.toast(errorStr);
			plus.nativeUI.closeWaiting();
		});
	}

	function liDateLenClicked() {
		var textareaE = document.getElementById('textarea');
		textareaE.blur();
		var stDate = document.getElementById('stDate');
		var stDateStr = stDate.innerText;
		if(stDateStr == '请选择') {
			mui.toast('请选择开始日期');
			return;
		}
		var endDate = document.getElementById('endDate');
		var endDateStr = endDate.innerText;
		if(endDateStr == '请选择') {
			mui.toast('请选择结束日期');
			return;
		}
		var dayCount = computeDatesInterval(stDateStr, endDateStr);
		if (dayCount < 0) {
			mui.toast('请选择结束日期');
			return;
		}else if (dayCount > 1) {
			mui.toast('请假多余1天,不可选择时长哦');
			return;
		}
		var dateLenInt = dayCount - 0.5;
		var pickData = [{
			'text': dayCount+'天'
		}, {
			'text': dateLenInt + '天'
		}];
		
		if (plus.os.name == 'iOS') {
			userpicker.setData(pickData,"请选择请假时长");
		}else{
			userpicker.setData(pickData);
		}
		userpicker.show(function(items) {
			var dateLen = document.getElementById('dateLen');
			var dateLenStr = items[0]['text'];
			dateLen.innerText = dateLenStr;
		});
	}

	function submitBtnClicked() {
		var absentCodeE = document.getElementById('absentCode');
		var indexStr = absentCodeE.getAttribute('data-index');
		var index = parseInt(indexStr);
		var dict = absentCodeArr[index];
		var absentCodeStr = absentCodeE.innerText;
		if(absentCodeStr == '请选择') {
			mui.toast('请选择请假类型');
			return;
		}
		var indexStr = absentCodeE.getAttribute('data-index');
		var index = parseInt(indexStr);
		var absentCodeDict = absentCodeArr[index];

		var stDate = document.getElementById('stDate');
		var stDateStr = stDate.innerText;
		if(stDateStr == '请选择') {
			mui.toast('请选择开始日期');
			return;
		}
		var endDate = document.getElementById('endDate');
		var endDateStr = endDate.innerText;
		if(endDateStr == '请选择') {
			mui.toast('请选择结束日期');
			return;
		}

		var dateLen = document.getElementById('dateLen');
		var dateLenStr = dateLen.innerText;
		dateLenStr = dateLenStr.substring(0,dateLenStr.length - 1);
		
		var textareaE = document.getElementById('textarea');
		var applyReason = textareaE.value;
		if(applyReason == '') {
			mui.toast('请填写申请理由');
			return
		}
		plus.nativeUI.showWaiting("正在提交",{modal:false});
		var PerId = getLocalData('pinfo').rw;
		
		var xmlStr = '<Request>^'+locId+'^'+PerId+'^'+dict['rw']+'^'+
			stDateStr+'^'+endDateStr+'^'+dateLenStr+'^'+applyReason+'</Request>';
		// console.log(xmlStr);
		postHttpRequest(xmlStr, 'nurse.stop.nurleavesave', function(result) {
			plus.nativeUI.closeWaiting();
			var resultDict = eval('(' + result + ')');
			if (resultDict['success'] == true) {
				plus.storage.setItem('detail','1');
				mui.toast('提交成功');
				mui.back();
			}else if (resultDict['msg'] != null || resultDict['msg'] != '') {
				mui.toast(resultDict['msg']);
			}else{
				mui.toast('提交失败,请重试');
			}
		},function(errorStr){
			mui.toast(errorStr);
			plus.nativeUI.closeWaiting();
		});
	}
	
	/*提交
	 * nurse.stop.nurleavesave
	 * rw^DepId(请假时护士所在病区)^PerId(请假护士人员Id)^LeaveType(请假类型)^StDate(请假开始日期)^EndDate(请假结束日期)^Days(请假时长(按天))^LeaveExplain(请假说明)
	 * 入参说明 rw申请时为空,LeaveType是休班代码中的rw
	 */
	
})(mui);